package com.stargazer.ming.ui;

import com.stargazer.ming.R;
import com.stargazer.ming.ui.base.MingButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * The base activity that every UI should be inherited
 * 
 * @author Ben
 * 
 */
public abstract class MingActivity extends Activity {
	public static final String TAG = "Ming.MingActivity";

	protected LayoutInflater mInflater;

	private View mContentView;
	private FrameLayout mUIContainer;
	private LinearLayout mRootLayout;
	
	//title
	private TextView mTitleTV=null;
	private ImageView mDropListIV=null;
	private MingButton prevBtn=null;
	private MingButton nextBtn=null;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mInflater = LayoutInflater.from(this);
		mContentView = (View) mInflater.inflate(R.layout.activity_ming, null);
		mUIContainer=(FrameLayout) mContentView.findViewById(R.id.ming_ui_container);
		mRootLayout = (LinearLayout) mContentView
				.findViewById(R.id.ming_root_layout);

		final int titleId = getTitleId();
		if (titleId != -1) {
			mRootLayout.addView(mInflater.inflate(titleId, null),
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			mTitleTV=(TextView) mRootLayout.findViewById(R.id.title);
			mDropListIV=(ImageView) mRootLayout.findViewById(R.id.droplist_iv);
			prevBtn=(MingButton) mRootLayout.findViewById(R.id.prev_button);
			nextBtn=(MingButton) mRootLayout.findViewById(R.id.next_button);
		}
		
		final int bodyId = getBodyId();
		if (bodyId != -1) {
			mRootLayout.addView(mInflater.inflate(bodyId, null),
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT);
		}
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(mContentView);
		
	}

	/**
	 * 
	 * @return the title view ID, or -1 if title view is not required
	 */
	protected int getTitleId() {
		return R.layout.ming_title;
	}
	
	public void addView(View child){
		mUIContainer.addView(child);
	}
	
	public void setMingTitle(final String title){
		if (mTitleTV != null) {
			mTitleTV.setText(title);
		}
	}
	
	public void setMingTitle(final int resId){
		setMingTitle(getString(resId));
	}
	
	public void setTitleDropListListener(View.OnClickListener l){
		mDropListIV.setVisibility(View.VISIBLE);
		mDropListIV.setOnClickListener(l);
		mTitleTV.setOnClickListener(l);
	}
	
	public MingButton setPrevBtn(final int imageId,final View.OnClickListener l){
		if(prevBtn==null){
			return null;
		}
		
		prevBtn.setVisibility(View.VISIBLE);
		prevBtn.setImageRsource(imageId);
		prevBtn.setOnClickListener(l);
		
		return prevBtn;
	}
	
	public MingButton setPrevBtn(final String text,final View.OnClickListener l){
		if(prevBtn==null){
			return null;
		}
		
		prevBtn.setVisibility(View.VISIBLE);
		prevBtn.setText(text);
		prevBtn.setOnClickListener(l);
		prevBtn.setBackgroundResource(R.drawable.title_back_btn_bg_normal);
		
		return prevBtn;
	}
	
	public MingButton setNextBtn(final int imageId,final View.OnClickListener l){
		if(nextBtn==null){
			return null;
		}
		
		nextBtn.setVisibility(View.VISIBLE);
		nextBtn.setImageRsource(imageId);
		nextBtn.setOnClickListener(l);
		
		return nextBtn;
	}
	
	public MingButton setNextBtn(final String text,final View.OnClickListener l){
		if(nextBtn==null){
			return null;
		}
		
		nextBtn.setVisibility(View.VISIBLE);
		nextBtn.setText(text);
		nextBtn.setOnClickListener(l);
		
		return nextBtn;
	}

	protected abstract void initView();

	protected abstract int getBodyId();
}
